﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bank
{
    using System;
    using System.Collections.Generic;

    // 枚举类型，表示账号类型
    enum AccountType
    {
        Savings,
        Checking,
        Credit
    }

    // 自定义异常类，表示有坏的钞票
    class BadCashException : Exception
    {
        public BadCashException(string message) : base(message)
        {
        }
    }

    // 账号类
    class Account
    {
        // 字段
        public string accountNumber;
        public double balance;
        public AccountType type;

        // 属性
        public string AccountNumber
        {
            get { return accountNumber; }
        }

        public double Balance
        {
            get { return balance; }
        }

        public Account(string accountNumber)
        {
            this.accountNumber = accountNumber;
            balance = 0.0;
        }

        // 方法：存款
        public void Deposit(double amount)
        {
            balance += amount;
        }

        // 方法：取款
        public virtual void Withdraw(double amount)
        {
            if (amount > balance)
            {
                throw new InvalidOperationException("Insufficient funds");
            }

            Random random = new Random();

            // 模拟抛出异常
            if (random.NextDouble() < 0.3) // 30% 的概率
            {
                throw new BadCashException("Bad Luck!!!! The cash is 坏的!!!");
            }

            balance -= amount;
        }
    }

    // 信用账号类，继承自账号类
    class CreditAccount : Account
    {
        // 新增字段
        private double creditLimit;

        // 新增属性
        public double CreditLimit
        {
            get { return creditLimit; }
        }

        // 构造方法
        public CreditAccount(string accountNumber, double creditLimit) : base(accountNumber)
        {
            this.creditLimit = creditLimit;
        }

        // 覆盖父类的取款方法，可以取更多钱钱
        public override void Withdraw(double amount)
        {
            if (amount > balance + creditLimit)
            {
                throw new InvalidOperationException("Exceeded credit limit");
            }

            Random random = new Random();

            // 模拟抛出异常
            if (random.NextDouble() < 0.3) // 30% 的概率
            {
                throw new BadCashException("Bad Luck!!!! The cash is 坏的!!!");
            }

            balance -= amount;
        }
    }

    // ATM类
    class ATM
    {
        // 取走大笔金额
        public event EventHandler<BigMoneyArgs> BigMoneyFetched;

        //取款
        public void WithdrawMoney(Account account, double amount)
        {
            if (amount > 10000)
            {
                OnBigMoneyFetched(account, amount);
            }

            try
            {
                account.Withdraw(amount);
                Console.WriteLine("Withdrawal successful. New balance: {0}", account.Balance);
            }
            //对象异常
            catch (InvalidOperationException ex)
            {
                Console.WriteLine("Withdrawal failed: {0}", ex.Message);
            }
            //自定义异常（坏钞）
            catch (BadCashException ex)
            {
                Console.WriteLine("Withdrawal failed: {0}", ex.Message);
            }
        }

        // 触发事件的方法
        protected virtual void OnBigMoneyFetched(Account account, double amount)
        {
            if (BigMoneyFetched != null)
            {
                BigMoneyArgs args = new BigMoneyArgs(account, amount);
                BigMoneyFetched(this, args);
            }
        }
    }

    // 事件参数类
    class BigMoneyArgs : EventArgs
    {
        public Account Account { get; private set; }
        public double Amount { get; private set; }

        public BigMoneyArgs(Account account, double amount)
        {
            Account = account;
            Amount = amount;
        }
    }

    //银行类
    class Bank
    {
        private Dictionary<string, Account> accounts;

        public Bank()
        {
            accounts = new Dictionary<string, Account>();
        }

        // 索引器
        public Account this[string accountNumber]
        {
            get
            {
                if (accounts.ContainsKey(accountNumber))
                {
                    return accounts[accountNumber];
                }
                else
                {
                    throw new ArgumentException("Account not found.");
                }
            }
            set
            {
                accounts[accountNumber] = value;
            }
        }
    }

    // 主程序
    class Program
    {
        static void Main(string[] args)
        {
            Bank bank = new Bank();

            Account savingsAccount = new Account("123456789");
            savingsAccount.Deposit(5000);

            Account creditAccount = new CreditAccount("987654321", 10000);

            ATM atm = new ATM();
            atm.BigMoneyFetched += Atm_BigMoneyFetched;

            atm.WithdrawMoney(savingsAccount, 2000);
            atm.WithdrawMoney(creditAccount, 8000);
            atm.WithdrawMoney(creditAccount, 15000);

            bank["123456789"] = savingsAccount; 
            bank["987654321"] = creditAccount;

            Console.WriteLine("Account 1 balance: {0}", bank["123456789"].Balance);
            Console.WriteLine("Account 2 balance: {0}", bank["987654321"].Balance);

            Console.ReadLine();
        }

        // 事件处理程序
        private static void Atm_BigMoneyFetched(object sender, BigMoneyArgs e)
        {
            Console.WriteLine("ALERT: Big money fetched! Account: {0}, Amount: {1}", e.Account.AccountNumber, e.Amount);
        }
    }
}
